package ldh.im.fxgui.controller.sub;

import com.jfoenix.controls.JFXDialog;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.data.pojo.status.ImMessageRefType;
import ldh.im.fxbase.util.BeanFactory;
import ldh.im.fxbase.util.ImCacheFactory;
import ldh.im.fxgui.pane.MessagePane;
import ldh.im.fxgui.pane.MessagePaneEnum;
import ldh.im.fxgui.service.MessageSendService;
import ldh.im.fxgui.util.UiUtil;
import org.controlsfx.control.PopOver;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TransferBaseController {

    @FXML protected Pane messageContainer;
    @FXML protected Label messageContentLabel;

    protected List<ImMessage> transferMessages;
    protected boolean isOneToOneTransfer = false; // 是否逐个转发

    protected PopOver popOver = new PopOver();

    protected JFXDialog dialog;

    protected MessageSendService messageSendService = BeanFactory.getInstance().getBean(MessageSendService.class);

    public void setDialog(JFXDialog dialog) {
        this.dialog = dialog;
    }

    public void setTransferImages(List<ImMessage> messages, boolean isOneToOneTransfer) {
        this.transferMessages = messages;
        this.isOneToOneTransfer = isOneToOneTransfer;
        String userId = UiUtil.getLoginImUser().getUserId();

        if (transferMessages.size() == 1) {
            ImMessage message = transferMessages.get(0);
            messageContainer.getChildren().setAll(buildOneMessagePane(userId, message));

        } else {
            String text = isOneToOneTransfer ? "【逐个转发】" : "【合并转发】";
            messageContainer.getChildren().setAll(new Label(text));
        }
    }

    @FXML
    protected void close(ActionEvent actionEvent) {
        if (dialog != null) {
            dialog.close();
        }
    }

    @FXML
    private void showMessageMoreContent(MouseEvent mouseEvent) {
        if (transferMessages == null) return;
        String userId = UiUtil.getLoginImUser().getUserId();

        if (transferMessages.size() == 1) {
            ImMessage transferMessage = transferMessages.get(0);
            Node node = buildOneMessagePane(userId, transferMessage);

            StackPane container = new StackPane(node);
            container.setPadding(new Insets(10, 10, 10, 10));
            popOver.setContentNode(container);
            popOver.setDetachable(false);
            popOver.setArrowLocation(PopOver.ArrowLocation.LEFT_CENTER);
            popOver.show(messageContentLabel, -10);
        } else {
            VBox vBox = new VBox();
            vBox.setPadding(new Insets(10, 10, 10, 10));
            vBox.setSpacing(10);
            for (ImMessage imMessage : transferMessages) {
                MessagePane messagePane = MessagePaneEnum.getByMessageType(imMessage.getMessageType()).getMessagePane();
                Node node = messagePane.createViewPane(null, imMessage, imMessage.isSender(userId));
                ImUser imUser = ImCacheFactory.getInstance().getUser(imMessage.getFromUserId());
                vBox.getChildren().add(new Label(imUser.getUserName() + "："));
                vBox.getChildren().add(node);
            }
            popOver.setContentNode(vBox);
            popOver.setDetachable(false);
            popOver.setArrowLocation(PopOver.ArrowLocation.LEFT_CENTER);
            popOver.show(messageContentLabel, -10);
        }

    }

    protected List<ImMessage> buildTransferMessage(ImSession imSession) {
        List<ImMessage> result = new ArrayList<>();
        if (isOneToOneTransfer) {
            for (ImMessage transferMessage : transferMessages) {
                String content = transferMessage.getContent();
                ImMessage message = messageSendService.buildMessage(imSession, content, MessageType.Text, (ImMessage) null);
                message.setMessageType(transferMessage.getMessageType());
                message.setExt(transferMessage.getExt());
                message.setImSession(imSession);
                if (transferMessage.isRef()) {
                    message.setRefType(transferMessage.getRefType());
                    message.setRefMessageIds(transferMessage.getRefMessageIds());
                }
                result.add(message);
            }
        } else if (transferMessages.size() > 1){
            ImMessage message = messageSendService.buildMessage(imSession, "", MessageType.Chat, null);
            message.setRefType(ImMessageRefType.transfer);
            message.setRefMessages(transferMessages);
            message.setRefMessageIds(transferMessages.stream().map(m->m.getServerId()).collect(Collectors.joining(",")));
            result.add(message);
        } else {
            throw new RuntimeException("不支持这种转发方式");
        }

        return result;
    }

    private Node buildOneMessagePane(String userId, ImMessage message) {
        MessagePane messagePane = MessagePaneEnum.getByMessageType(message.getMessageType()).getMessagePane();
        Node messageNode = messagePane.createViewPane(null, message, !message.isSender(userId));

        if (message.isRef()) {
            VBox vBox = new VBox();
            vBox.setSpacing(10);
            vBox.getChildren().add(messageNode);
            for (ImMessage refMessage : message.getRefMessages()) {
                MessagePane refMessagePane = MessagePaneEnum.getByMessageType(refMessage.getMessageType()).getMessagePane();
                Node refMessageNode = refMessagePane.createViewPane(null, refMessage, !refMessage.isSender(userId));
                vBox.getChildren().add(refMessageNode);
            }
            return vBox;
        }

        return messageNode;
    }
}
